package com.yxm.useryxm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.yxm.useryxm.model.domain.User;
import com.yxm.useryxm.model.domain.request.UserLoginRequest;
import com.yxm.useryxm.model.domain.request.UserRegisterRequest;
import com.yxm.useryxm.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.yxm.useryxm.contance.UserContance.*;


/**
 * 用户接口
 *
 * @author yxm
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("register")
    public Long userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            return null;
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassWord = userRegisterRequest.getUserPassWord();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassWord, checkPassword)) {
            return null;
        }
        return userService.userRegister(userAccount, userPassWord, checkPassword);
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest
     * @param request
     * @return
     */
    @PostMapping("login")
    public User userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            return null;
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassWord = userLoginRequest.getUserPassWord();
        if (StringUtils.isAnyBlank(userAccount, userPassWord)) {
            return null;
        }
        return userService.userLogin(userAccount, userPassWord, request);
    }

    @GetMapping("search")
    public List<User> userList(String userName, HttpServletRequest request) {
        if (!isAdmin(request)) {
            return new ArrayList<>();
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(userName)) {
            queryWrapper.like("user_name", userName);
        }
        List<User> userList = userService.list(queryWrapper);
        return userList.stream().map(
                user -> {
                    user.setUserPassword(null);
                    return userService.getSafetytUser(user);
                }
        ).collect(Collectors.toList());
    }


    @PostMapping("delete")
    public boolean deleteUser(@RequestBody long id, HttpServletRequest request) {
        if (!isAdmin(request)) {
            return false;
        }
        if (id < 0) {
            return false;
        }
        return userService.removeById(id);
    }

    /**
     * 是否为管理员
     *
     * @param request
     * @return
     */
    private static boolean isAdmin(HttpServletRequest request) {
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATUS);
        User user = (User) userObj;
        if (user == null || user.getUserRole() != ADMIN_ROLE) {
            return false;
        }
        return true;
    }
}
